package edu.sctu.xialei.june.day20150630one;

import java.sql.*;

/**
 * Created by Administrator on 2015/6/30.
 */
public class DatabaseUtil {

    public void linkDatabase() {
        Connection con;
        Statement sql;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        try {
            String uri = "jdbc:mysql://localhost:3306/namelist";
            con = DriverManager.getConnection(uri, "xialei", "huaixialei");
            sql = con.createStatement();
            rs = sql.executeQuery("SELECT * FROM dishes");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            while(rs.next()){
                try {
                    System.out.println( "菜名" + rs.getString(1));
                    System.out.println( "价格" + rs.getString(2));
                    System.out.println( "评价" + rs.getString(3));
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
